<!--
 * @Author: xiaodong 565071984@qq.com
 * @Date: 2022-05-08 19:52:05
 * @LastEditors: xiaodong 565071984@qq.com
 * @LastEditTime: 2022-05-12 21:01:56
 * @FilePath: /小票/src/views/accountManagement/userManagement/components/UserManagementEdit.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  > 
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <!-- <el-form-item label="代理商账号" prop="account">
        <el-input
          v-model.trim="form.account"
          autocomplete="off"
          disabled
        ></el-input>
      </el-form-item> -->
      <el-form-item label="分组名称" prop="name" :required="false">
        <el-input v-model.trim="form.name" autocomplete="off"></el-input>
      </el-form-item>
        <el-form-item label="选择玩法"   prop="sc_name">
        <el-select v-model="form.sc_name" placeholder="请选玩法" @change="changeScene">
          <el-option
            v-for="(item, index) in sceneList"
            :key="index"
            :label="item.name"
            :value="item.name"
          >{{item.name}}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备" prop="devs">
        <el-button @click="handleEdit('', 'deviceSelect')">选择</el-button>
        <!-- <el-input v-model.trim="form.iphone" autocomplete="off"></el-input> -->
      </el-form-item>
      <el-form-item label="">
        <el-tag v-for="tag in form.devs" :key="tag">
          {{ tag }}
        </el-tag>
        <!-- <el-input v-model.trim="form.email" autocomplete="off"></el-input> -->
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
    <device-select @set-account="onSetAccount " ref="deviceSelect" @fetch-data="fetchData" />
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/userManagement'
  import userData from '@/views/accountManagement/userManagement/data.json'
  import DeviceSelect from '@/views/dataStatistics/userManagement/components/deviceSelect.vue'
   import { getScenes,addGroup,editGroup } from '@/api/business.js'
  export default {
    name: 'UserManagementEdit',
    components: { DeviceSelect },
    data() {
      return {
        form: {
          name: '',
          password: '',
          email: '',
          permissions: [],
          devs:[]
        },
        rules: {
          username: [
            { required: true, trigger: 'blur', message: '请输入用户名' },
          ],
          password: [
            { required: true, trigger: 'blur', message: '请输入密码' },
          ],
          email: [{ required: true, trigger: 'blur', message: '请输入邮箱' }],
          permissions: [
            { required: true, trigger: 'blur', message: '请选择权限' },
          ],
        },
        title: '',
        sceneList:[],
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      onSetAccount(acclist){
        this.form.devs=acclist;
        console.log(acclist)
      },
        changeScene(e){
        this.form.sc_id=this.sceneList.filter(res=>{
          return res.name==e;
        })[0].sc_id;
      },
       getScenes(){
           let rq={
             limit:99,
             page:1
           }
          getScenes(rq).then(res=>{
            if(res.code==0){
              this.sceneList=res.data.data;
            }
          }) 
      },
      handleEdit(row, type = 'edit') {
        if (this.form.devs) {
          this.$refs[type].showEdit(this.form.devs)
        } else {
          this.$refs[type].showEdit()
        }
        this.getScenes();
      },
      async fetchData() {
        this.listLoading = true
        const { data, totalCount } = userData || (await getList(this.queryForm))
        console.log(data, totalCount, '获取数据')
        this.list = data
        this.total = totalCount
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
          console.log(this.form)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            let bmsg='';
            if(this.form.skj_id){
              const { msg } = await editGroup(this.form)
              bmsg=msg;
            }else{
              const { msg } = await addGroup(this.form)
              bmsg=msg;
            }
          
            this.$baseMessage(bmsg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
